<?php
namespace BoxAdmin\Controller;

use Think\Controller;

class AddonsController extends CommonController
{


    public function index()
    {
        $where = 1;
        if ($this->kw = I('kw')) {
            $where .= " and (name like '%$this->kw%' or  title like '%$this->kw%'  or  description like '%$this->kw%')";
        }
        $this->data = BoxModel("system_addons")->get("is_show=1");
        $this->display();
    }

    public function add_addons()
    {
        $model = BoxModel("system_addons");

        if (IS_POST) {
            $data = $model->create();
            $data['create_time'] = time();
            if ($data['id']) {
                $model->where(array('id' => $data['id']))->save($data);
            } else {
                $model->add($data);
            }
        }
        if ($id = I('id')) {
            $this->data = $model->load($id);
        }
        $this->display();
    }

    function del_addons()
    {
        if ($id = I('id')) {
            BoxModel("system_addons")->where(array('id' => $id))->save(array('is_show' => 2));
            $this->jump("删除成功");
        }

    }

    public function add()
    {
        $id = I('id');
        $this->memuNav = "index";
        $addon = BoxModel("system_addons")->load($id);
        $name = "SyncLogin";
        $this->addon_path = ADDONS_PATH . $name . '/';
        if (is_file($this->addon_path . 'config.php')) {
            $this->config_file = $this->addon_path . 'config.php';
        }
        $db_config = $addon['config'];
        $addon['config'] = include $this->config_file;
        if ($db_config) {
            $db_config = json_decode($db_config, true);
            foreach ($addon['config'] as $key => $value) {
                if ($value['type'] != 'group') {
                    $addon['config'][$key]['value'] = $db_config[$key];
                } else {
                    foreach ($value['options'] as $gourp => $options) {
                        foreach ($options['options'] as $gkey => $value) {
                            $addon['config'][$key]['options'][$gourp]['options'][$gkey]['value'] = $db_config[$gkey];
                        }
                    }
                }
            }
        }
        $this->data = $addon;
        $this->display();
    }

    /**
     * 保存插件设置
     */
    public function saveConfig()
    {
        $id = (int)I('id');
        $config = I('config');
        $flag = BoxModel("system_addons")->where("id={$id}")->setField('config', json_encode($config));

        if ($flag !== false) {
            $this->jump("保存成功");
        } else {
            $this->jump("保存失败");
        }
    }


    /*******勾子管理****/
    public function hook_index()
    {
        $where = 1;
        if ($this->kw = I('kw')) {
            $where .= " and (hook_name like '%$this->kw%' or  info like '%$this->kw%'  or  remark like '%$this->kw%')";
        }
        $this->data = BoxModel("system_hooks")->get($where, "", "hook_name desc");
        $this->display();
    }

    /*******勾子管理****/
    public function hook_add()
    {
        $hook_name = I('hook_name');
        $this->data = BoxModel("system_hooks")->load($hook_name);
        $model = BoxModel("system_hooks");
        if (IS_POST) {
            $data = $model->create();
            $data['addtime'] = date("Y-m-d H:i:s");
            $data['info'] = serialize($data['info']);
            if ($id = I('id')) {
                $model->where(array('id' => $id))->save($data);
            } else {
                $model->add($data);
            }
            $this->jump("操作成功", U('Addons/hook_index'));
        }
        $this->display();
    }


    public function hook_status()
    {
        $hook_name = I('hook_name');
        $this->data = BoxModel("system_hooks")->load($hook_name);
        $status = $this->data['status'] == 1 ? 2 : 1;
        BoxModel("system_hooks")->where(array('hook_name' => $hook_name))->save(array('status' => $status));
        $this->jump("操作成功");
    }

    public function hook_del()
    {
        $id = I('id');
        //$this->data=BoxModel("system_hooks")->load($id);
        BoxModel("system_hooks")->where(array('id' => $id))->delete();
        $this->jump("删除成功");
    }


}

?>